package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import com.project.util.InfoResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("user")
@Api(tags = "用户相关controller")
public class UserController {
    @Autowired
    private IUserService iUserService;

    @ApiModelProperty("分页查询带模糊查询")
    @GetMapping("showUserByPage/{pageNum}/{username}")
    private PageInfo<UserBean> showAllUserByPage(@PathVariable int pageNum,
                                                 @PathVariable String username) {
        return iUserService.showUserByPage(pageNum, username);
    }

    @ApiModelProperty("添加用户")
    @PostMapping("addUser/{username}/{realName}/{roles}")
    private InfoResult addUser(@PathVariable String realName,
                               @PathVariable int[] roles,
                               @PathVariable String username) {
        return iUserService.addUser(username, roles, realName);
    }

    @DeleteMapping("delUserById/{userId}")
    private InfoResult delUser(@PathVariable int userId) {
        return iUserService.delUserById(userId);
    }

    @GetMapping("showUserById/{userId}")
    private UserBean showUserById(@PathVariable int userId) {
        return iUserService.findUserById(userId);
    }

    @PostMapping("updateUserById/{userId}/{roles}")
    private InfoResult updateUserById(@PathVariable int userId, @PathVariable int[] roles) {
        return iUserService.updateUserById(userId, roles);
    }

    @GetMapping("userLogin/{username}/{password}")
    private UserBean userLogin( @PathVariable String username, @PathVariable String password) {
        return iUserService.userLogin(username, password);
    }

    @PostMapping("updateUserPwd/{userId}/{oldPassword}/{newPassword}")
    private InfoResult updateUserPwd(@PathVariable int userId,
                                     @PathVariable String oldPassword,
                                     @PathVariable String newPassword) {
        return iUserService.userUpdatePwd(userId, oldPassword, newPassword);
    }
}
